package com.example.k8s.hello.common.bean;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.k8s.hello.common.xss.SQLFilter;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Schema(description = "分页数据")
public class Page<T> implements IPage<T> {
    @Schema(description = "当前页，默认1", type = "integer", defaultValue = "1")
    protected long current = 1;

    @Schema(description = "每页显示条数，默认10，负数时不分页", type = "integer", defaultValue = "10")
    protected long size = 10;

    @Schema(description = "排序字段", type = "string", defaultValue = "")
    public String property;
    @Schema(description = "排序顺序，默认正序", type = "boolean", defaultValue = "true")
    public boolean asc = true;

    @Schema(description = "总数", type = "integer")
    @Parameter(hidden = true)
    protected long total = 0;

    @Schema(description = "查询数据列表")
    @Parameter(hidden = true)
    protected List<T> records = Collections.emptyList();

    public Page() {
    }

    public Page(long current, long size) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
    }

    @Override
    public List<OrderItem> orders() {
        if (StringUtils.isBlank(property)) {
            return null;
        }
        // 驼峰转数据库字段
        String underlineProperty = StringUtils.camelToUnderline(property);
        // 防止SQL注入
        underlineProperty = SQLFilter.sqlInject(underlineProperty);

        return Arrays.asList(asc ? OrderItem.asc(underlineProperty) : OrderItem.desc(underlineProperty));
    }

    @Override
    public List<T> getRecords() {
        return records;
    }

    @Override
    public Page<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public Page<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public Page<T> setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public Page<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    @Override
    @Schema(description = "总页数")
    public long getPages() {
        return IPage.super.getPages();
    }

    public String getProperty() {
        return property;
    }

    public Page<T> setProperty(String property) {
        this.property = property;
        return this;
    }

    public boolean isAsc() {
        return asc;
    }

    public Page<T> setAsc(boolean asc) {
        this.asc = asc;
        return this;
    }
}
